package com.sjs.flow.controller;

import cn.hutool.core.util.StrUtil;
import com.sjs.flow.entity.FlowProcess;
import com.sjs.flow.entity.vo.ExcuteProcessVo;
import com.sjs.flow.entity.vo.TaskVo;
import com.sjs.flow.http.exception.PlatformException;
import com.sjs.flow.http.response.DefaultResponse;
import com.sjs.flow.protocol.PlatformErrorCode;
import com.sjs.flow.service.FlowProcessService;
import com.sjs.flow.utils.EntityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author uto_dev_008
 * @desc
 * @since 2022/7/26 17:30
 * Copyright (c) 2021 南京友拓信息技术有限公司 All rights reserved
 */
@Api(tags = "流程操作")
@RestController
@RequestMapping("/flow/flowProcess")
public class FlowProcessController {

    @Autowired
    FlowProcessService flowProcessService;

    @Resource
    TaskService taskService;

    @ApiOperation("新增流程")
    @PostMapping("/add")
    public DefaultResponse<FlowProcess> add(@RequestBody(required = false) FlowProcess t) throws UnsupportedEncodingException {
        if (StrUtil.isEmpty(t.getBpmnXml())) {
            if (StrUtil.isEmpty(t.getBpmnPath())) {
                throw new PlatformException("参数为空", PlatformErrorCode.SYS_ERROR.getCode());
            } else {
                t.setBpmnXml(EntityUtil.base64Xml(t.getBpmnPath()));
            }
        }
        flowProcessService.add(t);
        return DefaultResponse.success(t);
    }

    @ApiOperation("启动流程")
    @PostMapping("/excuteProcess")
    public DefaultResponse<String> excuteProcess(@RequestBody(required = false) ExcuteProcessVo vo) {
        return DefaultResponse.success(flowProcessService.excuteProcess(vo));
    }

    @ApiOperation("获取所有流程")
    @GetMapping("/getFlowProcess")
    public DefaultResponse<List<FlowProcess>> getFlowProcess(String processKey) {
        return DefaultResponse.success(flowProcessService.getFlowProcess(processKey));
    }


    @ApiOperation("待处理任务")
    @GetMapping("/todoList")
    public DefaultResponse<List<TaskVo>> todoList(String id) {
        List<Task> taskList = taskService.createTaskQuery().taskCandidateOrAssigned(id).orderByTaskCreateTime().desc().list();
        List<Task> group = taskService.createTaskQuery().taskCandidateGroup(id).orderByTaskCreateTime().desc().list();
        taskList.addAll(group);
        List<TaskVo> result = new ArrayList<>();
        if (!CollectionUtils.isEmpty(taskList)) {
            taskList.forEach(task -> {
                TaskVo vo = new TaskVo();
                BeanUtils.copyProperties(task, vo);
                result.add(vo);
            });
        }
        return DefaultResponse.success(result);
    }

    @ApiOperation("已办任务")
    @GetMapping("/doneList")
    public DefaultResponse<List<Task>> doneList(String id) {
        //todo 代做
        return DefaultResponse.success(null);
    }

    @ApiOperation("任务处理")
    @PostMapping("/handle")
    public DefaultResponse<String> handle(@RequestBody TaskVo vo) {
        Task task = taskService.createTaskQuery().taskId(vo.getId()).singleResult();
        if (task == null) {
            throw new PlatformException("流程不存在", PlatformErrorCode.SYS_ERROR.getCode());
        }
        taskService.complete(vo.getId(), vo.getVars());
        return DefaultResponse.success(null);
    }

}
